package pkg

import (
	"lesson/consts"
	"log"

	"context"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

func UploadImg(dst, filename string) bool {
	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider(consts.ALI_AK_SECRET_KEY, consts.ALI_SK_SECRET_KEY)).
		WithRegion(consts.REGION_SECRET_KEY)
	// 创建OSS客户端
	client := oss.NewClient(cfg)
	// 创建上传管理器
	u := client.NewUploader()
	// 定义本地文件路径，需要替换为您的实际本地文件路径和文件名称
	localFile := dst
	// 执行上传文件的操作
	result, err := u.UploadFile(context.TODO(),
		&oss.PutObjectRequest{
			Bucket: oss.Ptr(consts.BUCKETNAME_SECRET_KEY),
			Key:    oss.Ptr(filename),
			Acl:    oss.ObjectACLPublicRead,
		},
		localFile)
	if err != nil {
		log.Fatalf("failed to upload file %v", err)
		return false
	}
	// 打印上传文件的结果
	log.Printf("upload file result:%#v\n", result)
	return true
}
